"""
依赖注入容器配置
使用 dependency-injector 管理服务依赖关系
"""

from dependency_injector import containers, providers
from sqlalchemy.orm import Session

from src.config.database import get_db
from src.services.auth_service import AuthService
from src.services.ticket_service import TicketService
from src.services.group_service import GroupService
# 将ResearchService的导入移入函数内部以避免循环导入
# from src.services.research_service import ResearchService
from src.services.database_metadata_service import DatabaseMetadataService
from src.services.generic_query_service import GenericQueryService
from src.services.database_session_service import DatabaseSessionService
from src.services.scheduler_service import SchedulerService
from src.services.feishu_service import FeishuService
from src.services.notification_service import NotificationService
from src.services.websocket_service import WebSocketService
from src.services.knowledge_graph_service import KnowledgeGraphService
from src.services.cache_service import CacheService
from src.services.research_cache import ResearchCache
from src.services.query_cache_service import QueryCacheService
from src.services.monitoring_service import MonitoringService
from src.services.security_service import RateLimiter, InputValidator
from src.services.service_registry import ServiceRegistry
from src.services.enhanced_collaboration_service import EnhancedCollaborationService
# 将redis_cache_service的导入移入函数内部以避免循环导入
# from src.services.cache_service import get_redis_cache_service
from src.services.user_profile_service import UserProfileService
from src.services.reinforcement_learning_service import get_reinforcement_learning_service
from src.services.intelligent_decision_service import get_intelligent_decision_service
from src.services.knowledge_base_service import KnowledgeBaseService  # 导入知识库服务

# 导入新增的学习服务
from src.services.enhanced_reinforcement_learning_service import get_enhanced_reinforcement_learning_service
from src.services.incremental_learning_service import (
    get_incremental_learning_service,
    get_online_learning_service,
    get_knowledge_evolution_service
)

from src.utils.nlp_enhancement import get_nlp_support, get_text_generator




class ServiceContainer(containers.DeclarativeContainer):
    """服务容器"""
    
    # 配置提供者
    config = providers.Configuration()
    
    # 数据库会话提供者 - 使用统一的 get_db 函数
    db_session = providers.Resource(get_db)
    
    # 服务注册表
    service_registry = providers.Singleton(ServiceRegistry)
    
    # 核心服务 - 按功能分组
    auth_service = providers.Factory(AuthService)
    ticket_service = providers.Factory(TicketService)
    group_service = providers.Factory(GroupService)
    # 延迟导入ResearchService以避免循环依赖
    research_service = providers.Factory(
        lambda: __import__('src.services.research_service', fromlist=['ResearchService']).ResearchService
    )
    
    # 数据服务
    database_metadata_service = providers.Factory(DatabaseMetadataService, db_session_factory=db_session)
    generic_query_service = providers.Factory(GenericQueryService, db_session_factory=db_session)
    knowledge_graph_service = providers.Factory(KnowledgeGraphService)
    
    # 通信服务
    feishu_service = providers.Factory(FeishuService)
    notification_service = providers.Factory(NotificationService, db_session_factory=db_session)
    websocket_service = providers.Factory(WebSocketService, db_session_factory=db_session)
    
    # 协作服务
    enhanced_collaboration_service = providers.Factory(EnhancedCollaborationService)
    
    # 缓存服务
    cache_service = providers.Factory(CacheService)
    redis_cache_service = providers.Factory(
        # 延迟导入以避免循环依赖
        lambda: __import__('src.services.cache_service', fromlist=['get_redis_cache_service']).get_redis_cache_service()
    )
    research_cache = providers.Factory(ResearchCache)
    query_cache_service = providers.Factory(QueryCacheService, db_session_factory=db_session)
    
    # 系统服务
    session_service = providers.Factory(DatabaseSessionService, db_session_factory=db_session)
    scheduler_service = providers.Factory(SchedulerService, db_session_factory=db_session)
    
    # 监控和安全服务
    monitoring_service = providers.Factory(MonitoringService)
    rate_limiter = providers.Factory(RateLimiter)
    input_validator = providers.Factory(InputValidator)
    
    # 知识库服务
    knowledge_base_service = providers.Factory(KnowledgeBaseService)  # 添加知识库服务提供者
    
    # 智能化服务
    user_preference_service = providers.Factory(UserProfileService, db_session=db_session)
    reinforcement_learning_service = providers.Factory(get_reinforcement_learning_service)
    intelligent_decision_service = providers.Factory(get_intelligent_decision_service)
    nlp_support = providers.Factory(get_nlp_support)
    text_generator = providers.Factory(get_text_generator)
    
    # 新增的学习服务
    enhanced_reinforcement_learning_service = providers.Factory(get_enhanced_reinforcement_learning_service)
    incremental_learning_service = providers.Factory(get_incremental_learning_service)
    online_learning_service = providers.Factory(get_online_learning_service)
    knowledge_evolution_service = providers.Factory(get_knowledge_evolution_service, knowledge_base_service=knowledge_base_service)


# 创建容器实例
container = ServiceContainer()


def init_container():
    """初始化容器"""
    pass


# 快捷访问函数
def get_service_registry():
    """获取服务注册表实例"""
    return container.service_registry()


def get_auth_service() -> AuthService:
    """获取认证服务"""
    return container.auth_service()


def get_research_service():
    """获取研究服务"""
    return container.research_service()


def get_ticket_service() -> TicketService:
    """获取工单服务"""
    return container.ticket_service()


def get_group_service() -> GroupService:
    """获取组服务"""
    return container.group_service()


def get_knowledge_graph_service() -> KnowledgeGraphService:
    """获取知识图谱服务"""
    return container.knowledge_graph_service()


def get_database_metadata_service() -> DatabaseMetadataService:
    """获取数据库元数据服务"""
    return container.database_metadata_service()


def get_generic_query_service() -> GenericQueryService:
    """获取通用查询服务"""
    return container.generic_query_service()


def get_session_service() -> DatabaseSessionService:
    """获取会话服务"""
    return container.session_service()


def get_scheduler_service() -> SchedulerService:
    """获取调度服务"""
    return container.scheduler_service()


def get_feishu_service() -> FeishuService:
    """获取飞书服务"""
    return container.feishu_service()


def get_notification_service() -> NotificationService:
    """获取通知服务"""
    return container.notification_service()


def get_websocket_service() -> WebSocketService:
    """获取WebSocket服务"""
    return container.websocket_service()


def get_enhanced_collaboration_service() -> EnhancedCollaborationService:
    """获取增强版协同编辑服务"""
    return container.enhanced_collaboration_service()


def get_cache_service() -> CacheService:
    """获取缓存服务"""
    return container.cache_service()


def get_redis_cache_service():
    """获取Redis缓存服务"""
    return container.redis_cache_service()


def get_research_cache() -> ResearchCache:
    """获取研究缓存服务"""
    return container.research_cache()


def get_query_cache_service() -> QueryCacheService:
    """获取查询缓存服务"""
    return container.query_cache_service()


def get_monitoring_service() -> MonitoringService:
    """获取监控服务"""
    return container.monitoring_service()


def get_rate_limiter() -> RateLimiter:
    """获取速率限制器"""
    return container.rate_limiter()


def get_input_validator() -> InputValidator:
    """获取输入验证器"""
    return container.input_validator()


def get_knowledge_base_service() -> KnowledgeBaseService:
    """获取知识库服务"""
    return container.knowledge_base_service()


def get_user_preference_service():
    """获取用户偏好服务"""
    return container.user_preference_service()


def get_reinforcement_learning_service():
    """获取强化学习服务"""
    return container.reinforcement_learning_service()


def get_intelligent_decision_service():
    """获取智能决策服务"""
    return container.intelligent_decision_service()


def get_nlp_support():
    """获取NLP支持服务"""
    return container.nlp_support()


def get_text_generator():
    """获取文本生成器服务"""
    return container.text_generator()
